package com.dongdong.android.tangramdemo.utils;


import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.util.Util;
import com.tmall.wireless.vaf.virtualview.Helper.ImageLoader;
import com.tmall.wireless.vaf.virtualview.view.image.ImageBase;


/**
 * Created by dongdongzheng on 2018/11/22.
 */

public class GlideVVSimpleTarget implements Target<Bitmap> {


    private int width = SIZE_ORIGINAL;
    private int height = SIZE_ORIGINAL;

    ImageBase mImageBase;

    ImageLoader.Listener listener;


    public GlideVVSimpleTarget(ImageBase mImageBase) {
        this.mImageBase = mImageBase;
    }

    public GlideVVSimpleTarget(ImageLoader.Listener listener) {
        this.listener = listener;
    }


    public void setSize(int width, int height) {
        if (!Util.isValidDimensions(width, height)) {
            Log.i("GlideVVSimpleTarget", "Width and height must be Target#SIZE_ORIGINAL or > 0");
        } else {
            this.width = width;
            this.height = height;
        }
    }


    @Override
    public void onLoadStarted(Drawable placeholder) {

    }

    @Override
    public void onLoadFailed(Exception e, Drawable errorDrawable) {
        if (mImageBase != null) {
            mImageBase.setImageDrawable(errorDrawable, true);
        }
        if (listener != null) {
            listener.onImageLoadSuccess(errorDrawable);
        }
    }

    @Override
    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
        if (mImageBase != null) {
            mImageBase.setBitmap(resource);
        }
        if (listener != null) {
            listener.onImageLoadSuccess(resource);
        }
    }

    @Override
    public void onLoadCleared(Drawable placeholder) {

    }

    @Override
    public void getSize(SizeReadyCallback cb) {
        cb.onSizeReady(width, height);
    }

    @Override
    public void setRequest(Request request) {

    }

    @Override
    public Request getRequest() {
        return null;
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onDestroy() {

    }


}
